package Controllers;

import Connection.User;
import Connection.WebPhaseLoader;
import Entities.Phase;
import StateInformation.GameState;
import SwingComponents.ArkanoidPanel;
import SwingComponents.CongratulationsPanel;

public class ArkanoidController {

    private PrincipalController principalController;
    private ArkanoidPanel arkanoidPanel;
    private CongratulationsPanel congratulationsPanel;
    private CongratulationsController congratulationsController;
    private Phase phase;
    private User user;
    
    public ArkanoidController(PrincipalController principalController, User user) {
        this.principalController = principalController;
        initArkanoidPanel();
        this.user = user;
        if (user != null && user.getGameState() != null) {
            phase = user.getGameState().getScene();
        }
        else {
            phase = new WebPhaseLoader(principalController.getApplication()).loadPhase(0, 0);
            user.setGameState(new GameState());
        }
    }
    
    public ArkanoidController(PrincipalController principalController, Phase phase, User user){
        congratulationsPanel = new CongratulationsPanel(principalController.getApplication());
        congratulationsController = new CongratulationsController(principalController, congratulationsPanel);
        user.setGameState(new GameState());
        this.user = user;
        this.principalController = principalController;
        initArkanoidPanel();
        this.phase = phase;
    }

    private void initArkanoidPanel() {
        arkanoidPanel = new ArkanoidPanel(this);
        principalController.getApplication().add(arkanoidPanel);
        arkanoidPanel.setVisible(true);
        arkanoidPanel.setFocusable(true);
    }
    
    public void execute(){
        PhaseController phaseController = new PhaseController(phase, this, user);
        arkanoidPanel.addKeyListener(phaseController);
        principalController.getApplication().addKeyListener(phaseController);
        phaseController.execute();
    }

    public ArkanoidPanel getArkanoidPanel() {
        return arkanoidPanel;
    }
    
    public void phaseFinished(PhaseController phaseController){
        phase = new WebPhaseLoader(principalController.getApplication()).loadPhase(phase.getWorldId(), phase.getId()+1);
        if(phase != null){
            //Desplegar fase
            execute();
        }else{
            //Era la última fase del mundo
            gameOver();
        }
    }
    
    public void gameOver() {
        arkanoidPanel.setVisible(false);
        congratulationsController.execute();
    }

    public PrincipalController getPrincipalController() {
        return principalController;
    }

    public void ballLost() {
        saveState();
    }

    private void saveState() {
//        if (!gameState.getUser().getName().equals("")) {
//            gameState.setWorld(world);
//            GameStateSaver gameSaver = new FileGameStateSaver();
//            gameSaver.save(gameState, gameState.getUser().getName());
//        }
    }
}
